package com.softtown.modules.sys.controller;

import com.github.pagehelper.PageInfo;
import com.softtown.common.annotation.LogOperation;
import com.softtown.common.exception.ErrorCode;
import com.softtown.common.utils.Result;
import com.softtown.modules.sys.dto.CompRoleUserAddDTO;
import com.softtown.modules.sys.dto.CompSysRoleUserAllocatePageDTO;
import com.softtown.modules.sys.dto.CompSysRoleUserDeleteDTO;
import com.softtown.modules.sys.service.CompSysRoleUserService;
import com.softtown.modules.sys.vo.RoleUserInfosVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 公司角色用户
 *
 * @author Dingyayong
 * @version v1.0
 * @create 2019-06-22 23:00
 **/
@RestController
@RequestMapping("comp/role/user")
@Api(tags = "公司角色用户")
public class CompSysRoleUserController {

    @Resource
    private CompSysRoleUserService compSysRoleUserService;

    private Logger logger = LoggerFactory.getLogger(getClass());

    @PostMapping("getRoleUsers")
    @ApiOperation("角色用户")
    @LogOperation("角色用户")
    public Result getRoleUsers(@RequestBody CompSysRoleUserAllocatePageDTO compSysRoleUserAllocatePageDTO) {
        logger.error(compSysRoleUserAllocatePageDTO.getPage().toString());
        return new Result<PageInfo<RoleUserInfosVO>>().ok(compSysRoleUserService.getCompUsersByRole(compSysRoleUserAllocatePageDTO));
    }

    @PostMapping("addRoleUser")
    @ApiOperation("添加用户")
    @LogOperation("添加用户")
    public Result addRoleUser(@RequestBody CompRoleUserAddDTO compRoleUserAddDTO) {
        Result result;
        result = compSysRoleUserService.checkRoleUserExist(compRoleUserAddDTO);
        if (compRoleUserAddDTO.getRoleId() == null) {
            compSysRoleUserService.insertRoleUser(compRoleUserAddDTO);
            return result.error(ErrorCode.ROLE_EMPTY_ERROR);
        }
        if (result == null) {
            compSysRoleUserService.insertRoleUser(compRoleUserAddDTO);
            return null;
        }
        return result;
    }

    @PostMapping("deleteRoleUser")
    @ApiOperation("删除用户")
    @LogOperation("删除用户")
    public Result deleteRoleUser(@RequestBody CompSysRoleUserDeleteDTO compSysRoleUserDeleteDTO) {
        return compSysRoleUserService.deleteCompRoleUser(compSysRoleUserDeleteDTO);
    }
}

